﻿$.postJSON = function (url, data, callback) {
    return $.ajax({
        url: url,
        data: JSON.stringify(data),
        type: "POST",
        processData: true,
        contentType: "application/json; charset=utf-8",
        timeout: 10000,
        dataType: "json",
        success: callback
    });
};

$.deleteJSON = function (url, data, callback) {
    return $.ajax({
        url: url,
        data: JSON.stringify(data),
        type: "DELETE",
        processData: true,
        contentType: "application/json; charset=utf-8",
        timeout: 10000,
        dataType: "json",
        success: callback
    });
};

/*    fancy textearea    */
(function ($) {

    var methods = {
       init: function (options) {
           this.each(function () {
               var fieldSet = $(this);
               var fake = $('<div class="fake-input"><p></p></div>');
               var className = fieldSet.attr('class');
               fake.addClass(className);
               fake.find('p').text(fieldSet.find('legend').text());
               var editorContainer = fieldSet.before(fake).hide();

               var editor = editorContainer.find('textarea').blur(function () {
                   if (this.value.length > 0)
                       return;
                   fake.show();
                   editorContainer.hide();
               }).change(function () {
                   if (this.value.length > 0 || $(this).is(":focus"))
                       return;
                   fake.show();
                   editorContainer.hide();
               });

               jQuery.data(this, 'fakeInput', fake);

               fake.click(function () {
                   fake.hide();
                   editorContainer.show();
                   editor.focus();
               });
           });
       },

       showInput: function() {
           var fieldSet = jQuery.data(this[0],'fakeInput');
           fieldSet.click();
       }
    };

    $.fn.fakeInput = function (methodOrOptions) {
        if ( methods[methodOrOptions] ) {
            return methods[methodOrOptions].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
            // Default to "init"
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        }   
    };
})(jQuery);

/*    subscribe/unsubscribe topic button    */
$.fn.subscribeUnsubscribeButton = function (options) {
    this.each(function () {
        if ($(this).data('topic') == null) {
            throw "Cannot create subscribeUnsubscribeButton without topic assigned";
        }

        var defaultOptions = {
            afterSubscribe: function () {
            },
            afterUnsubscribe: function () {
            }
        };

        if (typeof options == 'object') {
            options = $.extend(defaultOptions, options);
        } else {
            options = defaultOptions;
        }

        $(this).click(function () {
            var $button = $(this);

            if ($button.hasClass('busy')) {
                return;
            }

            var topic = $button.data('topic');

            $button.addClass('busy');

            if ($button.hasClass('subscribed')) {
                activityFeed.api.unsubscribe(topic).then(function () {
                    $button.removeClass('subscribed');
                }).always(function () {
                    $button.removeClass('busy');
                }).then(options.afterUnsubscribe);
            } else {
                activityFeed.api.subscribe(topic).then(function () {
                    $button.addClass('subscribed');
                }).always(function () {
                    $button.removeClass('busy');
                }).then(options.afterSubscribe);
            }
        });
    });
};
